package com.rimi.util;

import java.sql.*;

/**
 * @author jerry
 * JDBC工具类
 */
public class JdbcUtil {

    /**
     * 操作数据库的对象
     */
    private Connection connection;
    private Statement statement;
    private PreparedStatement preparedStatement;
    private ResultSet resultSet;


    /**
     * 获取连接
     */
    private void getConnection(){
        connection = PoolUtil.getConnection();
    }


    /**
     * 创建通道
     */
    private void creteStatement(){
        getConnection();
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            System.out.println("创建普通通道失败");
            e.printStackTrace();
        }
    }

    /**
     * 预编译通道
     */
    private void createPStatement(String sql){
        getConnection();
        try {
            preparedStatement = connection.prepareStatement(sql);
        } catch (SQLException e) {
            System.out.println("创建预编译通道失败");
            e.printStackTrace();
        }
    }

    /**
     * 预编译通道查询
     */
    public ResultSet executeQuerySql(String sql,Object[] params){
        createPStatement(sql);
        //绑定参数
        bandle(params);
        //执行语句
        try {
            return resultSet = preparedStatement.executeQuery();
        } catch (SQLException e) {
            System.out.println("预编译通道执行查询失败");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 绑定参数方法
     */
    private void  bandle(Object[] params){
        if(params == null){
            return;
        }
        try {
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setObject(i+1,params[i]);
            }
        } catch (SQLException e) {
            System.out.println("绑定参数失败");
            e.printStackTrace();
        }
    }

    /**
     * 预编译通道修改
     */
    public boolean executeUpdateSql(String sql,Object... params){
        createPStatement(sql);
        bandle(params);
        try {
            return preparedStatement.executeUpdate() > 0;
        } catch (SQLException e) {
            System.out.println("预编译通道执行修改语句失败");
            e.printStackTrace();
        }finally {
            this.closeRes();
        }
        return false;
    }

    /**
     * 执行修改语句
     */
    public boolean executeUpdateSql(String sql){
        creteStatement();
        try {
            return statement.executeUpdate(sql)>0;
        } catch (SQLException e) {
            System.out.println("执行修改语句失败");
            e.printStackTrace();
        } finally {
            this.closeRes();
        }
        return false;
    }
    /**
     * 执行查询语句
     * 需要使用结果集以后才能关闭资源
     */
    public ResultSet executeQuerySql(String sql){
        creteStatement();
        try {
            //离线结果集
            return resultSet=statement.executeQuery(sql);
        } catch (SQLException e) {
            System.out.println("执行查询语句失败");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 关闭资源
     */
    public void closeRes(){
        try {
            if(resultSet!=null){
                resultSet.close();
            }
            if(preparedStatement!=null){
                preparedStatement.close();
            }
            if(statement!=null){
                statement.close();
            }
            if(connection!=null){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
